package com.paad.todolist;

import java.text.SimpleDateFormat;
import android.content.Context;
import java.util.*;
import android.view.*;
import android.widget.*;

public class ShoppinglistEntityAdapter extends ArrayAdapter<ShoppinglistEntity> {

  int resource_id;

  public ShoppinglistEntityAdapter(Context _context, 
                             int _resource_id, 
                             List<ShoppinglistEntity> _items) {
    super(_context, _resource_id, _items);
    resource_id = _resource_id;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    LinearLayout checklistView;

    ShoppinglistEntity item = getItem(position);

    String checklistName = item.getName();
    Date createdDate = item.getCreated();
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");
    String dateString = sdf.format(createdDate);

    if (convertView == null) {
      checklistView = new LinearLayout(getContext());
      String inflater = Context.LAYOUT_INFLATER_SERVICE;
      LayoutInflater vi = (LayoutInflater)getContext().getSystemService(inflater);
      vi.inflate(resource_id, checklistView, true);
    } else {
      checklistView = (LinearLayout) convertView;
    }

    TextView dateView = (TextView)checklistView.findViewById(R.id.rowDate);
    TextView taskView = (TextView)checklistView.findViewById(R.id.row);
      
    dateView.setText(dateString);
    taskView.setText(checklistName);

    return checklistView;
  }
}